<?php
include_once 'protocol_includes.php';

class ChatMessage extends subapplication
{
    private $cmd;
    public static $PROTOCOL = "ChatMessage";

    public static function create(command $cmd)
    {
        if (!self::IsThisClass($cmd)) return null;
        $N = new ChatMessage($cmd);
        return $N;
    }


    public function __construct(command $cmd)
    {
        $this->cmd  = $cmd;
        $this->Username($cmd->GetData('Username'));
        $this->Datetime($cmd->GetData('Datetime'));
        $this->Text($cmd->GetData('Text'));
    }

    public function execute()
    {
        $this->UpdateResult('Chat message '.$this->cmd);

    }


    // =========== PROPERTIES ==========

    private $property = array();
    private function GetSetData($property_name,$value = null)
    {
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    public function Username($value = null)
    {
        return $this->GetSetData("Name",$value);
    }

    public function Datetime($value = null)
    {
        return $this->GetSetData("Datetime",$value);
    }

    public function Text($value = null)
    {
        return $this->GetSetData("Text",$value);
    }


    public function result()
    {
        $this->cmd->GetResult();
    }

    public function UpdateResult($str)
    {
        $this->cmd->SetResult($str);
    }



    /*
     * check to see if command is a UPLOAD
     */
    public static function IsThisClass(command $cmd)
    {

        if (!$cmd->HasDataKey('Usrename')) return false;
        if (!$cmd->HasDataKey('Datetime')) return false;
        if (!$cmd->HasDataKey('Text')) return false;

        return true;
    }


}

?>
